<template>
  <div class="app-container">
    <el-card class="card-h-100">
      <div slot="header">
        <span>密码修改</span>
      </div>
      <el-row :gutter="20">
        <el-col :span="12" :offset="6">
          <el-form ref="myform" :model="formItem" status-icon :rules="rules" label-position="top" label-width="100px">
            <!-- <el-form-item label="原始密码" prop="pass">
              <el-input v-model="formItem.pass" type="password" autocomplete="off" />
            </el-form-item> -->
            <el-form-item label="原始密码" prop="oldPassword">
              <el-input v-model="formItem.oldPassword" type="password" autocomplete="off" />
            </el-form-item>
            <el-form-item label="新密码" prop="password">
              <el-input v-model="formItem.password" type="password" autocomplete="off" />
            </el-form-item>
            <el-form-item label="确认新密码" prop="checkPassword">
              <el-input v-model="formItem.checkPassword" type="password" autocomplete="off" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" size="mini" icon="el-icon-check" @click="save()">保存</el-button>
              <el-button size="mini" icon="el-icon-refresh-left" @click="reset()">重置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import { changepassword } from '@neu/biz-core/api/user'
// 定义一个表单原始项
const formItem = {
  pass: '',
  oldPassword: '',
  password: '',
  checkPassword: ''
}
export default {
  name: 'Password',
  components: {
    //
  },
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.formItem.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      formItem: { // 编辑的表单内容
        ...formItem
      },
      rules: {
        oldPassword: [
          { required: true, message: '原始密码不能为空', trigger: 'blur' },
          { min: 6, max: 18, message: '6~18位密码', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 18, message: '6~18位密码', trigger: 'blur' }
        ],
        checkPassword: [
          { required: true, message: '确认新密码不能为空', trigger: 'blur' },
          { validator: validatePass, trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    //
  },
  created() {
    // 初始化数据
    this.fetch()
  },
  methods: {
    fetch() { // 初始化数据
      //
    },
    reset() {
      this.$refs.myform.resetFields()
    },
    // 保存操作，返回promise，后续处理交由调用者处理
    save() {
      const _this = this
      this.$refs.myform.validate((valid) => {
        if (valid) {
          changepassword(this.formItem).then(response => {
            this.$message({
              showClose: true,
              message: '修改成功, 请重新登录！',
              type: 'success'
            })
            setTimeout(function() {
              _this.logout()
            }, 2000)
          })
        } else {
          this.$message({
            showClose: true,
            message: '操作失败',
            type: 'error'
          })
        }
      })
    },
    async logout() {
      await this.$store.dispatch('user/logout')
      this.$router.push('/login')
    }
  }
}
</script>
